<?php

/**
 * @author Cesar Augustus Silva <cesaraugustussilva@linuxmail.org>
 * @copyright Copyright (c) 2013, Cesar Augustus Silva
 * @license http://www.gnu.org/licenses/gpl.html GNU GPL v3.0
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <­http://www.gnu.org/licenses/>.
 */

if ($_POST) {
    require_once(dirname(__FILE__).'/../config/config.inc.php');
    
    if ($_POST['login'] == 'access') {
        $email = $_POST['email'];
        $password = md5($_POST['password']);
        $persistent = $_POST['persistent'];
        if (Util::is_email($email)) {
            switch (Util::login_account($email, $password, $persistent)) {
                case 1:
                    echo 'SUCCESS';
                    break;
                case 0:
                    echo 'INVALID';
                    break;
                case -1:
                    echo 'INACTIVE';
                    break;
            }
        }
    }
    elseif ($_POST['login'] == 'register') {
        $email = $_POST['email'];
        $cpfcnpj = preg_replace('/\D/s', '', $_POST['cpfcnpj']);
        if (Util::is_email($email) && Util::is_cpfcnpj($cpfcnpj)) {
            if (Util::email_exists($email))
                echo 'EMAIL_EXISTS';
            elseif (Util::cpfcnpj_exists($cpfcnpj))
                echo 'CPFCNPJ_EXISTS';
            else
                echo 'SUCCESS';
        }
    }
}